<?php
/**
 * 
 * Digitalus2 CMS
 *
 * @package    	Digitalus\Navigation
 * @copyright  	Copyright (c) 2009,  Digitalus Media USA (http://digitalusmedia.net)
 * @license    	New BSD License, library/Dig/License.txt
 * @version 	2.0
 * 
 */
class DigNavigation_Service_Url
{
    /**
     * parses the url's for the local requests
     * these services use standard routing rather than ZF convention
     * this makes it much easier to integrate with different technologies
     * which all respect the path?param=value format
     * 
     * you can optionally pass it an array of params, which it appends to 
     * any GET parameters.
     *
     * @package Digitalus\Navigation
     * @param string $url, must be absolute!
     * @param array $params
     * @return Zend_Controller_Request
     */
    public static function parse($url, $params = array())
    {
        if(empty($url)) {
           return false;
        }
        
        $parsedUrl = parse_url($url);
        $path = $parsedUrl['path'];

        $params = self::getParams($path, $params);
        
        $getParams = null;
        if(!empty($parsedUrl['query'])) {
            $paramString = $parsedUrl['query'];
            parse_str($paramString, $getParams);
        }
        
        if(is_array($getParams)) {
            $params = array_merge($params, $getParams);
        }
        
        $pathParts = explode('/', $path);
        if($pathParts[0] == '') {
            array_shift($pathParts);
        }
        $request = new Zend_Controller_Request_Simple(
            isset($pathParts[2])?$pathParts[2]:'index' ,
            isset($pathParts[1])?$pathParts[1]:'index' ,
            isset($pathParts[0])?$pathParts[0]:null,
            $params
        );
        
        $parsedUrl = new stdClass();
        $parsedUrl->pathParts = $pathParts;
        $parsedUrl->path = $path;
        $parsedUrl->params = $params;
        $parsedUrl->request = $request;
        return $parsedUrl;
    }

    public static function stripParams($url) {
        $paramSeparator = Dig_Service_Module::settings('digNavigation', 'paramSeparator');

        // determine if the param separator is found, and explode on this as well
        if(strpos($url, $paramSeparator)) {
            $pathParts = explode($paramSeparator, $url);
            $url = $pathParts[0];
        }

        return $url;
    }

    public static function getParams($path, $params = array()) {

        $paramSeparator = Dig_Service_Module::settings('digNavigation', 'paramSeparator');

        // determine if the param separator is found, and explode on this as well
        if(strpos($path, $paramSeparator)) {
            $pathParts = explode($paramSeparator, $path);
            $path = $pathParts[0];
            if(!empty($pathParts[1])) {
                $urlParams = explode('/', $pathParts[1]);
                if(count($urlParams) > 0) {
                    for($i = 0; $i < count($urlParams); $i++) {
                        $key = $urlParams[$i];
                        $i++;
                        $value = $urlParams[$i];
                        $params[$key] = $value;
                    }
                }
            }
            unset($pathParts);
        }
        return $params;
    }
    
}
?>